/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.util;

import ohos.agp.colors.RgbColor;
import ohos.agp.utils.Color;

/**
 * 颜色转换工具类
 *
 * @since 2021-03-24
 */
public final class MyUtils {
    private static final int R_COLOR = 0xff0000;
    private static final int G_COLOR = 0x00ff00;
    private static final int B_COLOR = 0x0000ff;
    private static final int DISPLACEMENT_16 = 16;
    private static final int DISPLACEMENT_8 = 8;
    private static final int INDEX_0 = 0;
    private static final int INDEX_1 = 1;
    private static final int INDEX_2 = 2;

    /**
     * MyUtils
     */
    private MyUtils() {
    }

    /**
     * Color int类型转RGB
     *
     * @param colorValue
     * @return int Color转换成 int[]
     */
    public static int[] toRgb(int colorValue) {
        int red = (colorValue & R_COLOR) >> DISPLACEMENT_16;
        int green = (colorValue & G_COLOR) >> DISPLACEMENT_8;
        int blue = colorValue & B_COLOR;
        return new int[]{red, green, blue};
    }

    /**
     * String转换成RGB
     *
     * @param colorValue
     * @return RgbColor
     */
    public static RgbColor getRgbColor(String colorValue) {
        int[] colorlist = toRgb(Color.getIntColor(colorValue));
        RgbColor rgbColor = new RgbColor(colorlist[INDEX_0], colorlist[INDEX_1], colorlist[INDEX_2]);
        return rgbColor;
    }

    /**
     * int转换成RGB
     *
     * @param colorValue
     * @return RgbColor
     */
    public static RgbColor getRgbColor(int colorValue) {
        int[] colorlist = toRgb(colorValue);
        RgbColor rgbColor = new RgbColor(colorlist[INDEX_0], colorlist[INDEX_1], colorlist[INDEX_2]);
        return rgbColor;
    }
}
